<template>
    <div>
        <!-- ComingSoon page is a children of Film -->
        <ul>
            <li v-for="data in comingSoonListGetter" :key="data.filmId" @click="handleClick(data.isPresale, data.filmId)">
                <!-- <li v-for="data in $store.state.comingSoonListState" :key="data.filmId" @click="handleClick(data.filmId)"> -->
                <img :src="data.poster" />
                <h4>
                    {{ data.name }}
                    <span>{{ data.item.name }}</span>
                </h4>
                <p v-if="data.grade">
                    观众评分
                    <span>{{ data.grade }}</span>
                </p>
                <p>主演：{{ data.actors | actorsFilter }}</p>
                <p>{{ data.nation }} | {{ data.runtime }}分钟</p>
            </li>
        </ul>
    </div>
</template>

<script>
import Vue from 'vue';
import { mapGetters } from 'vuex';
import { MessageBox } from 'mint-ui';

Vue.filter('actorsFilter', (data) => {
    if (data) {
        let newActors = data.map((item) => item.name).join(' ');
        const len = newActors.length;
        if (len > 17) {
            newActors = newActors.substring(0, 17) + '...';
        }
        return newActors;
    } else {
        return '暂无主演';
    }
});

export default {
    data() {
        return {
            dataList: []
        };
    },
    computed: {
        ...mapGetters(['comingSoonListGetter'])
    },
    mounted() {
        if (this.$store.state.comingSoonListState.length === 0) {
            this.$store.dispatch('getComingSongAction');
        } else {
            console.log('获取的store缓存数据，没有发送请求！');
        }
    },
    methods: {
        handleClick(isPresale, filmId) {
            if (!isPresale) {
                MessageBox({
                    title: '提示',
                    message: '该影片还没有排期，请看看其它影片吧?',
                    showCancelButton: true
                }).then((res) => {
                    if (res === 'confirm') {
                        this.$router.push('/film/nowPlaying');
                    }
                });
            }

            this.$router.push({
                name: 'Detail-FilmId',
                params: { FilmId: filmId }
            });
        }
    }
};
</script>

<style lang="scss" scoped>
div {
    margin-left: 10px;
    margin-bottom: 40px;
    ul {
        padding: 5px;
        li {
            list-style: none;
            width: 100%;
            height: 120px;
            border-bottom: 1px solid #eeeeee;
            padding-top: 10px;
            cursor: pointer;
            h4 {
                margin: 0px;
                padding-top: 8px;
                font-weight: normal;
                span {
                    background: #dddddd;
                    font-size: 12px;
                    color: #ffffff;
                }
            }
            img {
                width: 66px;
                height: 93px;
                float: left;
                margin-right: 10px;
            }
            p {
                font-size: 13px;
                color: #797d82;
                margin: 0px;
                span {
                    color: lightsalmon;
                }
            }
        }
    }
}
</style>
